package com.egg.mall.rabbitmq.sender;

import com.alibaba.fastjson.JSON;
import com.egg.common.core.constant.rabbit.RabbitRoutingKey;
import com.egg.common.util.IdGenUtil;
import com.egg.mall.config.ReadConfig;
import com.egg.mall.constant.MallRabbitExchange;
import com.egg.mall.model.rabbit.SkuOrderTimeoutRabbitSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 发送
 *
 * @author east
 */
@Slf4j
@Component
public class SkuOrderSender {

    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送-订单取消
     *
     * @param orderId 订单ID
     */
    public void orderDelayCancel(Long orderId) {
        SkuOrderTimeoutRabbitSender param = new SkuOrderTimeoutRabbitSender();
        param.setOrderId(orderId);
        String messageId = IdGenUtil.strId();
        String jsonInfo = JSON.toJSONString(param);
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setMessageId(messageId);
        // 延迟关键
        messageProperties.setHeader("x-delay", ReadConfig.orderCancelDelayTime());
        Message message = new Message(jsonInfo.getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(
                MallRabbitExchange.ORDER_TIMEOUT_EXCHANGE
                , RabbitRoutingKey.CANCEL
                , message
        );
    }


}
